<?php

class Mpermission extends CI_Model {
	
	public $table = 'permission';
	public $pri_key="per_id";


	function __construct()

	{

		parent::__construct();

	}
	public function get_module()
	{
		return $this->db->get("module")->result_array();
	}
	public function get_module_from_lvl($lvl)
	{
		$this->db->where($this->pri_key." =",$lvl);
		$a =$this->db->get($this->table)->result_array();
		
		if(count($a) >0  && !empty($a))
		{
			$data_sess = array("area"=>$a[0]["per_area"]);
			$this->session->set_userdata($data_sess);
		}
		if($a[0]["per_module"] == "fullcontrol" )
			$m = $a[0]["per_module"];
		else
			$m = unserialize($a[0]["per_module"]);
	//	echo $m;
		return $m;
	}
	public function insert($data){

		if($this->db->insert($this->table,$data))
			return true;
		
		return false;

	}
		public function update($data,$id)
	{
		//$this->db->where[ar_where] = "";
		$where = $this->pri_key." =".$id;
		if($this->db->update($this->table,$data,$where))
			return true;
		
		return false;
	
	}
	
	public function info_from_id($id)
	{
		if(is_numeric($id) && $id !="")
		{
		$this->db->where($this->pri_key.' =',$id);
		return $this->db->get($this->table)->result_array();
		}
		else
			return false;
	}
	public function check_email($email)
	{
		$this->db->where('user_email =',$email);
		if($this->db->count_all($this->table) >0)
			return true; // email nay da ton tai va ko su dung duoc
		return false; // co the su dung email nay
			
 	}
	public function list_data($num,$off)
	{
		$this->db->limit($off,$num);
		
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
		
	
	return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	// dem so record 
	function count_all()
	{
		return $this->db->count_all_results($this->table);
	}
	
	public function check_username($username,$id='')
	{
		$sql="select * from user where user_name = '".$username."'";
		//$this->db->where('user_email =',$email);
		if($id != '')
		{
			//$this->db->where("user_id !=",$id);
			$sql= $sql." and user_id != ".$id;
			
		}
		$q= $this->db->query($sql);
	
		if($q->num_rows() >0)
			return false; //co the su dung email nay 
		return true; // email nay da ton tai va ko su dung duoc
			
 	}
	
	function resource_create()

	{

		//missing

		$expected = array(

      'avatar',

      'active',

      'last_name',

      'first_name',

      'date_join',

      'phone',

      'address',

      'email',

      'password',

      'last_login'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //clear text

    $last_name = htmlentities($last_name, ENT_QUOTES, 'UTF-8');

    $first_name = htmlentities($first_name, ENT_QUOTES, 'UTF-8');

    $full_name = $last_name.' '.$first_name;

    $address = htmlentities($address, ENT_QUOTES, 'UTF-8');

    $active = ($active=='null') ? 'no' : 'yes';

    $password = md5($password);

 

		//create

    $sql = "INSERT INTO human_resources SET

       avatar = '$avatar',

       active = '$active',

       last_name = '$last_name',

       first_name = '$first_name',

       full_name = '$full_name',

       date_join = '$date_join',

       phone = '$phone',

       address = '$address',

       email = '$email',

       password = '$password',

       last_login = '$last_login'

    ";

		if($this->db->query($sql)) return true;

    else return false;

	}



	function resource_review($begin, $limit)

	{

		$sql = "SELECT id, avatar, date_join,

                   last_name, first_name, full_name, phone, address,

                   active, email, last_login

            FROM human_resources

            ORDER BY id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}

		

		return false;

	}



  function resource_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT id, avatar, date_join,

                   last_name, first_name, full_name, phone, address,

                   active, email, last_login

            FROM human_resources WHERE ";

    if ($type == 'id')

      $sql .= "id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



	function resource_update($id) //return true/false

	{

		//missing

		$expected = array(

      'avatar',

      'active',

      'last_name',

      'first_name',

      'date_join',

      'phone',

      'address',

      'email',

      'password',

      'last_login'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //clear text

    $last_name = htmlentities($last_name, ENT_QUOTES, 'UTF-8');

    $first_name = htmlentities($first_name, ENT_QUOTES, 'UTF-8');

    $full_name = $last_name.' '.$first_name;

    $address = htmlentities($address, ENT_QUOTES, 'UTF-8');

    $active = ($active=='null') ? 'no' : 'yes';



    //kiem tra password co thay doi khong

    $row = $this->info_from_id($id);

    $old_pass = $row['password'];

    if ($old_pass!=$password)

      $password = md5($password);

    else

      $password = $old_pass;



    //update

     $sql = "UPDATE human_resources SET

                         avatar = '$avatar',

                         active = '$active',

                         last_name = '$last_name',

                         first_name = '$first_name',

                         full_name = '$full_name',

                         date_join = '$date_join',

                         phone = '$phone',

                         address = '$address',

                         email = '$email',

                         password = '$password',

                         last_login = '$last_login'

              WHERE id=$id

    ";



		if($this->db->query($sql)) return true;

    else return false;

	}



	function delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

			//	$row = $this->info_from_id($id);

		

				//xoa file

		//		if ($row['avatar'] != 'null') unlink($row['avatar']);

		

				//xoa CSDL

			//	$sql = "DELETE FROM human_resources WHERE id=$id";
				if($id >2 )
				{
				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 
				}//$this->db->query($sql);

			}			

		}

	}



  /*===================BAN ĐƯƠNG NHIỆM===================*/

  function bdn_review($begin, $limit) //DS Ban đương nhiệm

	{

		$sql = "SELECT dn.id, hr.active,

                   hr.full_name, hr.email, hr.phone,

                   dn.note

            FROM human_resources hr

            INNER JOIN human_duong_nhiem dn

            ON dn.id_human_resource = hr.id

            ORDER BY hr.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function bdn_create()

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }



		//create

    $sql = "INSERT INTO human_duong_nhiem SET

       id_human_resource = '$id_human_resource',

       note = '$note'

    ";

		if($this->db->query($sql)) return true;

    else return false;

	}



  function bdn_update($id) //return true/false

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );



		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //update

     $sql = "UPDATE human_duong_nhiem SET

                    id_human_resource = '$id_human_resource',

                    note = '$note'

             WHERE id=$id

    ";



		if($this->db->query($sql)) return true;

    else return false;

	}



  function bdn_delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				//xoa CSDL

				$sql = "DELETE FROM human_duong_nhiem WHERE id=$id";

				$this->db->query($sql);

			}

		}

	}



  function bdn_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT dn.id, hr.avatar, hr.date_join,

                   hr.last_name, hr.first_name, hr.full_name, hr.phone, hr.address,

                   hr.active, hr.email, hr.last_login

            FROM human_resources hr, human_duong_nhiem dn

            WHERE dn.id_human_resource = hr.id AND ";

    if ($type == 'id')

      $sql .= "hr.id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY dn.id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



 
  /*===================DS GIỮ TÀI SẢN===================*/

  function taisan_review($begin, $limit)

	{

		$sql = "SELECT ts.id, hr.active,

                   hr.full_name, hr.email, hr.phone,

                   ts.note

            FROM human_resources hr

            INNER JOIN human_tai_san ts

            ON ts.id_human_resource = hr.id

            ORDER BY hr.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function taisan_create()

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }



		//create

    $sql = "INSERT INTO human_tai_san SET

       id_human_resource = '$id_human_resource',

       note = '$note'

    ";

		if($this->db->query($sql)) return true;

    else return false;

	}



  function taisan_update($id) //return true/false

	{

		//missing

		$expected = array(

      'id_human_resource',

      'note'

    );



		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

		}



    //update

     $sql = "UPDATE human_tai_san SET

                    id_human_resource = '$id_human_resource',

                    note = '$note'

             WHERE id=$id

    ";



		if($this->db->query($sql)) return true;

    else return false;

	}



  function taisan_delete()

	{

		if (isset($_POST['select_id']))

		{

			foreach($_POST['select_id'] as $id) {

				//xoa CSDL

				$sql = "DELETE FROM human_tai_san WHERE id=$id";

				$this->db->query($sql);

			}

		}

	}



  function taisan_search()

	{

		$keyword = htmlentities($this->input->post('keyword'), ENT_QUOTES, 'UTF-8');

    $type = $this->input->post('search_type');

		$sql = "SELECT ts.id, hr.avatar, hr.date_join,

                   hr.last_name, hr.first_name, hr.full_name, hr.phone, hr.address,

                   hr.active, hr.email, hr.last_login

            FROM human_resources hr, human_tai_san ts

            WHERE ts.id_human_resource = hr.id AND ";

    if ($type == 'id')

      $sql .= "hr.id = '$keyword' ";

    elseif ($type == 'first_name')

      $sql .= "first_name LIKE '%$keyword%' ";

    elseif ($type == 'full_name')

      $sql .= "full_name LIKE '%$keyword%' ";



    $sql .= "ORDER BY ts.id DESC";



		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}






  /*===================DS THAM GIA SỰ KIỆN===================*/

  function event_review($id_event, $begin, $limit, $active='')

	{

		$sql = "SELECT he.id, hr.id AS id_user, hr.active,

                   hr.full_name, hr.email, hr.phone

            FROM human_resources hr, human_event he, events

            WHERE he.id_event = $id_event

                  AND he.id_human_resource = hr.id

                  AND he.id_event = events.id

            ORDER BY he.id DESC

            LIMIT $begin,$limit

		";

		$Q = $this->db->query($sql);

		if ($Q->num_rows() > 0)

		{

			return $Q->result_array();

		}



		return false;

	}



  function event_create()

	{

		//missing

		$expected = array(

      'id_event',

      'id_human_resource'

    );

		foreach ($expected as $value)

		{

			if (array_key_exists($value, $_POST))

      {

        ${$value} = $_POST[$value];

      }

      else

      {

        ${$value} = 'null';

      }

    }

  

    //kiem tra chuoi id_human_resource la mot chuoi ngan cach nhau boi dau ';'

    $a_id = explode(';', $id_human_resource);

    foreach ($a_id as $value) //chuỗi id_human_resource hợp lệ;

    {

      $value = trim($value);

      $sql = "INSERT INTO human_event SET

        id_event = '$id_event',

        id_human_resource = '$value'

      ";

      if (!$this->db->query($sql)) return false;

    }



    return true;

	}







  
}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>